home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / encodings / idna.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  245 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import stringprep
  5. import re
  6. import codecs
  7. from unicodedata import ucd_3_2_0 as unicodedata
  8. dots = re.compile(u'[.\xe3\x80\x82\xef\xbc\x8e\xef\xbd\xa1]')
  9. ace_prefix = 'xn--'
  10. uace_prefix = unicode(ace_prefix, 'ascii')
  11.  
  12. def nameprep(label):
  13.     newlabel = []
  14.     for c in label:
  15.         if stringprep.in_table_b1(c):
  16.             continue
  17.         
  18.         newlabel.append(stringprep.map_table_b2(c))
  19.     
  20.     label = u''.join(newlabel)
  21.     label = unicodedata.normalize('NFKC', label)
  22.     for c in label:
  23.         if stringprep.in_table_c12(c) and stringprep.in_table_c22(c) and stringprep.in_table_c3(c) and stringprep.in_table_c4(c) and stringprep.in_table_c5(c) and stringprep.in_table_c6(c) and stringprep.in_table_c7(c) and stringprep.in_table_c8(c) or stringprep.in_table_c9(c):
  24.             raise UnicodeError('Invalid character %r' % c)
  25.         stringprep.in_table_c9(c)
  26.     
  27.     RandAL = map(stringprep.in_table_d1, label)
  28.     for c in RandAL:
  29.         if c:
  30.             if filter(stringprep.in_table_d2, label):
  31.                 raise UnicodeError('Violation of BIDI requirement 2')
  32.             filter(stringprep.in_table_d2, label)
  33.             if not RandAL[0] or not RandAL[-1]:
  34.                 raise UnicodeError('Violation of BIDI requirement 3')
  35.             not RandAL[-1]
  36.             continue
  37.     
  38.     return label
  39.  
  40.  
  41. def ToASCII(label):
  42.     
  43.     try:
  44.         label = label.encode('ascii')
  45.     except UnicodeError:
  46.         pass
  47.  
  48.     if len(label) < len(label):
  49.         pass
  50.     elif len(label) < 64:
  51.         return label
  52.     raise UnicodeError('label empty or too long')
  53.     label = nameprep(label)
  54.     
  55.     try:
  56.         label = label.encode('ascii')
  57.     except UnicodeError:
  58.         pass
  59.  
  60.     if len(label) < len(label):
  61.         pass
  62.     elif len(label) < 64:
  63.         return label
  64.     raise UnicodeError('label empty or too long')
  65.     if label.startswith(uace_prefix):
  66.         raise UnicodeError('Label starts with ACE prefix')
  67.     label.startswith(uace_prefix)
  68.     label = label.encode('punycode')
  69.     label = ace_prefix + label
  70.     if len(label) < len(label):
  71.         pass
  72.     elif len(label) < 64:
  73.         return label
  74.     raise UnicodeError('label empty or too long')
  75.  
  76.  
  77. def ToUnicode(label):
  78.     if isinstance(label, str):
  79.         pure_ascii = True
  80.     else:
  81.         
  82.         try:
  83.             label = label.encode('ascii')
  84.             pure_ascii = True
  85.         except UnicodeError:
  86.             pure_ascii = False
  87.  
  88.     if not pure_ascii:
  89.         label = nameprep(label)
  90.         
  91.         try:
  92.             label = label.encode('ascii')
  93.         except UnicodeError:
  94.             raise UnicodeError('Invalid character in IDN label')
  95.         except:
  96.             None<EXCEPTION MATCH>UnicodeError
  97.         
  98.  
  99.     None<EXCEPTION MATCH>UnicodeError
  100.     if not label.startswith(ace_prefix):
  101.         return unicode(label, 'ascii')
  102.     label1 = label[len(ace_prefix):]
  103.     result = label1.decode('punycode')
  104.     label2 = ToASCII(result)
  105.     if label.lower() != label2:
  106.         raise UnicodeError('IDNA does not round-trip', label, label2)
  107.     label.lower() != label2
  108.     return result
  109.  
  110.  
  111. class Codec(codecs.Codec):
  112.     
  113.     def encode(self, input, errors = 'strict'):
  114.         if errors != 'strict':
  115.             raise UnicodeError('unsupported error handling ' + errors)
  116.         errors != 'strict'
  117.         if not input:
  118.             return ('', 0)
  119.         result = []
  120.         labels = dots.split(input)
  121.         if labels and len(labels[-1]) == 0:
  122.             trailing_dot = '.'
  123.             del labels[-1]
  124.         else:
  125.             trailing_dot = ''
  126.         for label in labels:
  127.             result.append(ToASCII(label))
  128.         
  129.         return ('.'.join(result) + trailing_dot, len(input))
  130.  
  131.     
  132.     def decode(self, input, errors = 'strict'):
  133.         if errors != 'strict':
  134.             raise UnicodeError('Unsupported error handling ' + errors)
  135.         errors != 'strict'
  136.         if not input:
  137.             return (u'', 0)
  138.         if isinstance(input, unicode):
  139.             labels = dots.split(input)
  140.         else:
  141.             input = str(input)
  142.             unicode(input, 'ascii')
  143.             labels = input.split('.')
  144.         if labels and len(labels[-1]) == 0:
  145.             trailing_dot = u'.'
  146.             del labels[-1]
  147.         else:
  148.             trailing_dot = u''
  149.         result = []
  150.         for label in labels:
  151.             result.append(ToUnicode(label))
  152.         
  153.         return (u'.'.join(result) + trailing_dot, len(input))
  154.  
  155.  
  156.  
  157. class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
  158.     
  159.     def _buffer_encode(self, input, errors, final):
  160.         if errors != 'strict':
  161.             raise UnicodeError('unsupported error handling ' + errors)
  162.         errors != 'strict'
  163.         if not input:
  164.             return ('', 0)
  165.         labels = dots.split(input)
  166.         trailing_dot = u''
  167.         if labels:
  168.             if not labels[-1]:
  169.                 trailing_dot = '.'
  170.                 del labels[-1]
  171.             elif not final:
  172.                 del labels[-1]
  173.                 if labels:
  174.                     trailing_dot = '.'
  175.                 
  176.             
  177.         
  178.         result = []
  179.         size = 0
  180.         for label in labels:
  181.             result.append(ToASCII(label))
  182.             if size:
  183.                 size += 1
  184.             
  185.             size += len(label)
  186.         
  187.         result = '.'.join(result) + trailing_dot
  188.         size += len(trailing_dot)
  189.         return (result, size)
  190.  
  191.  
  192.  
  193. class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
  194.     
  195.     def _buffer_decode(self, input, errors, final):
  196.         if errors != 'strict':
  197.             raise UnicodeError('Unsupported error handling ' + errors)
  198.         errors != 'strict'
  199.         if not input:
  200.             return (u'', 0)
  201.         if isinstance(input, unicode):
  202.             labels = dots.split(input)
  203.         else:
  204.             input = str(input)
  205.             unicode(input, 'ascii')
  206.             labels = input.split('.')
  207.         trailing_dot = u''
  208.         if labels:
  209.             if not labels[-1]:
  210.                 trailing_dot = u'.'
  211.                 del labels[-1]
  212.             elif not final:
  213.                 del labels[-1]
  214.                 if labels:
  215.                     trailing_dot = u'.'
  216.                 
  217.             
  218.         
  219.         result = []
  220.         size = 0
  221.         for label in labels:
  222.             result.append(ToUnicode(label))
  223.             if size:
  224.                 size += 1
  225.             
  226.             size += len(label)
  227.         
  228.         result = u'.'.join(result) + trailing_dot
  229.         size += len(trailing_dot)
  230.         return (result, size)
  231.  
  232.  
  233.  
  234. class StreamWriter(Codec, codecs.StreamWriter):
  235.     pass
  236.  
  237.  
  238. class StreamReader(Codec, codecs.StreamReader):
  239.     pass
  240.  
  241.  
  242. def getregentry():
  243.     return codecs.CodecInfo(name = 'idna', encode = Codec().encode, decode = Codec().decode, incrementalencoder = IncrementalEncoder, incrementaldecoder = IncrementalDecoder, streamwriter = StreamWriter, streamreader = StreamReader)
  244.  
  245.